<template>
  <view v-if="goodInfos.goods_name" style="padding-bottom: 40px;">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, index) in goodInfos.pics" :key="index">
        <image :src="item.pics_big" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    <view class="good-info-box">
      <view class="price">￥{{goodInfos.goods_price}}</view>
      <!-- 商品名称 -->
      <view class="goods-info-body">
        <view class="goods-name">{{goodInfos.goods_name}}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yf">快递：免运费</view>
    </view>
    <rich-text :nodes="goodInfos.goods_introduce"></rich-text>
    <view class="goods-nav">
      <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodInfos:{},
        options: [{
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 2
        }],
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        },
        {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }
        ]
      };
    },
    methods:{
      async queryGoodInfo(id){
        const res = await uni.$http.get('/api/public/v1/goods/detail', { goods_id:id })
        if (res.data.meta.status !== 200) return uni.$showMsg()
        res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img /g, "<img style='display: block' ")
        this.goodInfos = res.data.message
      },
      preview(index){
        uni.previewImage({
          current: index,
          urls: this.goodInfos.pics.map(item => item.pics_big)
        })
      },
      onClick (e) {
        if(e.index === 1){
          console.log('111');
          uni.switchTab({
            url:"/pages/cart/cart"
          })
        }
      },
      buttonClick (e) {
        console.log(e)
      }
    },
    onLoad(options) {
      let id = options.goods_id
      this.queryGoodInfo(id)
    }
  }
</script>

<style lang="scss">
  swiper{
    height: 750rpx;
    image{
      width: 100%;
      height: 100%;
    }
  }
  .good-info-box{
    padding: 12rpx;
    .price{
      color: #C00000;
    }
    .goods-info-body{
      display: flex;
      padding: 10px 0;
    }
    .goods-name{
      font-size: 14px;
    }
    .favi{
      width: 80px;
      display: flex;
      flex-direction: column;
      align-items: center;
      font-size: 12px;
    }
    .yf{
      color: gray;
      font-size: 12px;
    }
  }
  .goods-nav{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
